//
//  ProjectInfoViewController.m
//  F50
//
//  Created by zack on 15/5/28.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "ProjectInfoViewController.h"
#import "ProjectInfoViewCell.h"
#import "RoothomeDataModle.h"
@interface ProjectInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    int isrelation;
    UITableView *_tableView;
    NSArray *titleArr;
    NSMutableArray *filesArr;
    RoothomeDataModle *infoModle;
}
@end

@implementation ProjectInfoViewController
- (void)createBackground
{
    UIView *nav_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    nav_bg.backgroundColor = RGB(59, 107, 178);
    [self.view addSubview:nav_bg];
    
    //中间标题  聊天对象的名称
    
    UILabel *nav_title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 80, 25, 160, 30)];
    nav_title.text = self.modle.title;
    nav_title.textAlignment = NSTextAlignmentCenter;
    nav_title.font = [UIFont boldSystemFontOfSize:20];
    nav_title.textColor = [UIColor whiteColor];
    nav_title.backgroundColor = [UIColor clearColor];
    [nav_bg addSubview:nav_title];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(20, 32, 15, 18);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [nav_bg addSubview:backBtn];
    
 
    
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64)];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    if (WIDTH > 320) {
        self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT+64);
    }else{
        self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT+100);
    }
    
    
    self.scrollView.delegate = self;
    [self.scrollView setScrollEnabled:YES];//设置pageControl
    self.scrollView.bounces = NO;
    [self.view addSubview:self.scrollView];
    
    self.photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 70, 70)];
//    self.photoImage.image = [UIImage imageNamed:@"profile"];
    self.photoImage.layer.cornerRadius = 35;
    self.photoImage.layer.masksToBounds = YES;
    [self.scrollView addSubview:self.photoImage];
    
    
    self.nameLab = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 180, 30)];
    self.nameLab.font = [UIFont systemFontOfSize:18];
    self.nameLab.textColor = [UIColor whiteColor];
    [self.scrollView addSubview:self.nameLab];
    
 
    
    self.industry = [[UILabel alloc]initWithFrame:CGRectMake(90, 40, WIDTH - 20 - 80 - 10, 20)];
    self.industry.font = [UIFont systemFontOfSize:14];
    self.industry.textAlignment = NSTextAlignmentLeft;
    self.industry.textColor = RGB(134, 133, 133);
    [self.scrollView addSubview:self.industry];
    
    
    self.marks = [[UILabel alloc]initWithFrame:CGRectMake(90, 60, WIDTH - 20 - 80 - 10, 20)];
    self.marks.font = [UIFont systemFontOfSize:14];
    self.marks.textAlignment = NSTextAlignmentLeft;
    self.marks.textColor = RGB(134, 133, 133);
    [self.scrollView addSubview:self.marks];
    
#pragma mark -- 开始定义融资到分享的view
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH, 60)];
    aView.backgroundColor = RGB(3, 20, 28);
//    aView.alpha = 0.5;
    [self.scrollView addSubview:aView];
    
    self.amount = [[UILabel alloc]initWithFrame:CGRectMake(2, 0, WIDTH/3, 30)];
    self.amount.textColor = [UIColor whiteColor];
    self.amount.textAlignment = NSTextAlignmentCenter;
    [aView addSubview:self.amount];
    
    
    self.deals = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH/3, 30)];
    self.deals.textColor = [UIColor whiteColor];
    self.deals.textAlignment = NSTextAlignmentCenter;
    [aView addSubview:self.deals];
    
    self.relations = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH*2/3, 0, WIDTH/3, 30)];
    self.relations.textColor = [UIColor whiteColor];
    self.relations.textAlignment = NSTextAlignmentCenter;
    [aView addSubview:self.relations];
    NSArray *arr = [NSArray arrayWithObjects:@"融资",@"洽谈",@"关注", nil];
    NSArray *btnarr = [NSArray arrayWithObjects:@"分享",@"洽谈",@"关注", nil];
    NSArray *btnImage = [NSArray arrayWithObjects:@"share@2x",@"doChat@2x",@"relation@2x", nil];

    for (int i = 0; i< 3; i++) {
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH*i/3, 30, WIDTH/3, 30)];
        lab.text = arr[i];
        lab.textColor = [UIColor whiteColor];
        lab.textAlignment = NSTextAlignmentCenter;
        [aView addSubview:lab];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(WIDTH*i/3, 160, WIDTH/3-2, 50);
        btn.backgroundColor = RGB(59, 107, 178);
        [btn setImage:[UIImage imageWithImageName:btnImage[i]] forState:UIControlStateNormal];
        [btn setTitle:btnarr[i] forState:UIControlStateNormal];

        if (i==2 && [self.modle.isrelation intValue]) {
            [btn setTitle:@"取消关注" forState:UIControlStateNormal];
            [btn setImage:nil forState:UIControlStateNormal];
        }
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        btn.tag = 2000+i;
        [btn addTarget:self action:@selector(doChinceAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:btn];
        
        
    }
    
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 210, WIDTH, HEIGHT - 64 -210) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = RGB(35, 91, 133);
    _tableView.scrollEnabled = NO; //设置tableview 不能滚动
    [self.scrollView addSubview:_tableView];
    

    
    
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    titleArr = [NSArray arrayWithObjects:@"项目简介",@"创始人",@"项目文件", nil];
    [self.photoImage sd_setImageWithURL:[NSURL URLWithString:self.modle.imageUrl] placeholderImage:[UIImage imageNamed:@"profile"]];
    
    
    isrelation = [self.modle.isrelation intValue];
    filesArr = [[NSMutableArray alloc]init];

    
    NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"token"],@"token",self.modle.projectID,@"id",nil];
    NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"project",@"class",@"projectinfo",@"func",date,@"param", nil];
    
    [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
        NSLog(@"%@",result);
        NSLog(@"%@",[result objectForKey:@"result"]);
//        NSArray *array = [result objectForKey:@"result"];
        NSDictionary * dict= [NSDictionary dictionaryWithDictionary:[result objectForKey:@"result"]];

        
//        for (int i = 0; i < result.count; i++) {
            infoModle=[[RoothomeDataModle alloc] init];
            infoModle.title = [dict objectForKey:@"title"];
            infoModle.imageUrl = [dict objectForKey:@"thumb"];
            infoModle.projectID = [dict objectForKey:@"id"];
            infoModle.releasetime = [dict objectForKey:@"releasetime"];
            infoModle.info = [dict objectForKey:@"description"];
            infoModle.stage = [dict objectForKey:@"stage"];
            infoModle.state = [dict objectForKey:@"state"];
            infoModle.relations = [dict objectForKey:@"relations"];
            infoModle.deals = [dict objectForKey:@"deals"];
            infoModle.amount = [dict objectForKey:@"amount"];
            infoModle.isrelation = [dict objectForKey:@"isrelation"];
            infoModle.industry = [dict objectForKey:@"industry"];
            
            infoModle.marks = [dict objectForKey:@"marks"];
            infoModle.founder = [dict objectForKey:@"founder"];
            infoModle.founderinfo = [dict objectForKey:@"founder"];
        filesArr = [dict objectForKey:@"files"];
//            [dataArr addObject:modle];
//        }

        self.nameLab.text = infoModle.title;
        self.industry.text = infoModle.industry;
        self.marks.text = [NSString stringWithFormat:@"项目评分：%@",infoModle.marks];
        self.deals.text = infoModle.deals;
        self.relations.text = infoModle.relations;
        self.amount.text = infoModle.amount;

        
        
        [_tableView reloadData];
        
    } fail:^{
        
    }];

    

}
-(void)viewDidAppear:(BOOL)animated{
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createBackground];
}


#pragma mark ----- uitableviewdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifier = @"cell";
    ProjectInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[ProjectInfoViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.chanceLab.text = titleArr[indexPath.section];
    switch (indexPath.section) {
        case 0:{
            cell.infoLab.text = infoModle.info;
            [cell setInfoLabelTextFrame:infoModle.info];
        }
            break;
        case 1:{
            cell.infoLab.text = infoModle.founder;
            [cell setInfoLabelTextFrame:infoModle.founder];
        }
            break;
        case 2:{
            cell.infoLab.hidden = YES;
            for (int i = 0; i<filesArr.count ; i++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame = CGRectMake(WIDTH*i/3, 40, WIDTH/3-2, 60);
                
                
                [btn setImage:[UIImage imageWithImageName:@"file@2x"] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:18];
//                btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
                btn.tag = 2000+i;
                [btn addTarget:self action:@selector(seeProjectFile:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn];
            }
        }
            break;
            
        default:
            break;
    }
   
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return [ProjectInfoViewCell heightForCell:infoModle.info];
    }else if (indexPath.section == 1){
       return [ProjectInfoViewCell heightForCell:infoModle.founder];
    }
 
    return 70;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    view.backgroundColor = RGB(12, 62, 102);
    return  view;
}


- (void)doChinceAction:(UIButton*)sender{
        NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"token"],@"token",self.modle.projectID,@"id",nil];
    switch (sender.tag) {
        case 2002:{
            UIButton *btn = (UIButton *)[self.view viewWithTag:2002];
            if (isrelation) {
                isrelation = 0;
                self.relations.text = [NSString stringWithFormat:@"%d",[self.relations.text intValue] - 1];
                [btn setTitle:@"关注" forState:UIControlStateNormal];
                [btn setImage:[UIImage imageWithImageName:@"relation@2x"] forState:UIControlStateNormal];
                
                
                NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"relation",@"class",@"remove",@"func",date,@"param", nil];
                
                [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
                    NSLog(@"%@",[result objectForKey:@"errMsg"]);
                    NSLog(@"%@",result);
                    
                } fail:^{
                    
                }];

            }else{
                isrelation = 1;
                self.relations.text = [NSString stringWithFormat:@"%d",[self.relations.text intValue] + 1];
                [btn setTitle:@"取消关注" forState:UIControlStateNormal];
                [btn setImage:nil forState:UIControlStateNormal];
            
                NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"relation",@"class",@"relation",@"func",date,@"param", nil];
                
                [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
                    NSLog(@"%@",[result objectForKey:@"errMsg"]);
                    NSLog(@"%@",result);
                    
                    
                } fail:^{
                    
                }];

            
            }
            
        }
            break;
            
        default:
            break;
    }
}

- (void)seeProjectFile:(UIButton *)sender{
    int numOfFile = (int)sender.tag - 2000;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[[filesArr objectAtIndex:numOfFile] objectForKey:@"url"]]];
    [[UIApplication sharedApplication] openURL:url];
}


- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
